package com.mystudy.management.usermanagement.laytablemessageconvert;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mystudy.management.usermanagement.dao.pojo.RequestTrace;
import com.mystudy.management.usermanagement.dao.pojo.VisitTrace;
import com.mystudy.management.usermanagement.dao.pojo.Visitor;
import com.mystudy.management.usermanagement.dao.pojo.VisitorLimit;
import com.sun.istack.internal.NotNull;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


//数据表返回数据打包工具
@Component
public class LayuiTableMessageConvert {


    /*layui
    默认规定的数据格式code
        {
            "code": 0,
            "msg": "",
            "count": 1000,
            "data": [{}, {}]
        }
     */

    /*
    -------------------------------------授权管理界面-------------------------------------------
     */

    //用户对查询结果进行打包的方法
    public Object indexMessagePacking(Collection<Visitor> result){
        if(result == null){
            return getObjectVisitor(false, true, 0, JSON.toJSONString(null), null);
        }
        return getObjectVisitor(true, result.isEmpty(), result.size(), JSON.toJSONString(result), result);

    }

    @NotNull
    private Object getObjectVisitor(boolean b, boolean empty2, int size, String string, Collection<Visitor> result) {
        int count;
        JSONArray data;
        if( b &&!empty2){
            count = size;
            data = JSONArray.parseArray(string);
            JSONObject media;
            for (int i = 1; i<= size; i++){
                media = data.getJSONObject(i-1);
                media.put("index",i);
                data.set(i-1,media);//为每个消息加上序号
            }


        }else{
            count = 0;
            Collection<Visitor> empty = new ArrayList<>();
            data = JSONArray.parseArray(JSON.toJSONString(empty));
        }
        JSONObject responseMessage = new JSONObject();
        responseMessage.put("code",200);
        responseMessage.put("msg","");
        responseMessage.put("count",count);
        responseMessage.put("data",data);
        System.out.println(responseMessage);
        return responseMessage;
    }


    @NotNull
    private Object getObjectVisitTrace(boolean b, boolean empty2, int size, String string, Collection<VisitTrace> result) {
        int count;
        JSONArray data;
        if( b &&!empty2){
            count = size;
            data = JSONArray.parseArray(string);
            JSONObject media;
            for (int i = 1; i<= size; i++){
                media = data.getJSONObject(i-1);
                media.put("index",i);
                media.put("visitDate",(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(media.get("visitDate")));
                data.set(i-1,media);//为每个消息加上序号
            }

        }else{
            count = 0;
            Collection<Visitor> empty = new ArrayList<>();
            data = JSONArray.parseArray(JSON.toJSONString(empty));
        }
        JSONObject responseMessage = new JSONObject();
        responseMessage.put("code",200);
        responseMessage.put("msg","");
        responseMessage.put("count",count);
        responseMessage.put("data",data);
        System.out.println(responseMessage);
        return responseMessage;
    }

    @NotNull
    private Object getObjectRequestTrace(boolean b, boolean empty2, int size, String string, Collection<RequestTrace> result) {
        int count;
        JSONArray data;
        if( b &&!empty2){
            count = size;
            data = JSONArray.parseArray(string);
            JSONObject media;
            for (int i = 1; i<= size; i++){
                media = data.getJSONObject(i-1);
                media.put("index",i);
                media.put("traceDate",(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(media.get("traceDate")));
                data.set(i-1,media);//为每个消息加上序号
            }

        }else{
            count = 0;
            Collection<Visitor> empty = new ArrayList<>();
            data = JSONArray.parseArray(JSON.toJSONString(empty));
        }
        JSONObject responseMessage = new JSONObject();
        responseMessage.put("code",200);
        responseMessage.put("msg","");
        responseMessage.put("count",count);
        responseMessage.put("data",data);
        System.out.println(responseMessage);
        return responseMessage;
    }

    /*
    ------------------------------------------------------访问监管 - 访问记录--------------------------------------------------
     */

    //对查询结果进行打包的方法
    public Object index2messagePacking(Collection<VisitTrace> result){
        if(result == null){
            return getObjectVisitTrace(false, true, 0, JSON.toJSONString(null), null);
        }
        return getObjectVisitTrace(true, result.isEmpty(), result.size(), JSON.toJSONString(result), result);

    }

    public Object index2messagePacking(List<RequestTrace> result){
        if(result == null){
            return getObjectRequestTrace(false, true, 0, JSON.toJSONString(null), null);
        }
        return getObjectRequestTrace(true, result.isEmpty(), result.size(), JSON.toJSONString(result), result);

    }


    /*
    ---------------------------------------------------访问监管 - 访问控制------------------------------------------------------
     */

    public Object index2_2messagePacking(Collection<VisitorLimit> result){
        int count = 0;
        JSONArray data;
        if(!result.isEmpty()){
             data = JSON.parseArray(JSON.toJSONString(result));
            JSONObject media;
            for (int i = 1; i<= data.size(); i++){
                media = data.getJSONObject(i-1);
                media.put("index",i);
                media.put("startDate",(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(media.get("startDate")));
                media.put("endDate",(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(media.get("endDate")));
                data.set(i-1,media);//为每个消息加上序号
            }
        }else{
            Collection<Visitor> empty = new ArrayList<>();
            data = JSONArray.parseArray(JSON.toJSONString(empty));
        }
        JSONObject responseMessage = new JSONObject();
        responseMessage.put("code",200);
        responseMessage.put("msg","");
        responseMessage.put("count",count);
        responseMessage.put("data",data);
        System.out.println(responseMessage);
        return responseMessage;
    }


    public Object index2_2messagePacking(List<Visitor> result){
        int count = 0;
        JSONArray data;
        if(!result.isEmpty()){
            data = JSON.parseArray(JSON.toJSONString(result));
            JSONObject media;
            for (int i = 1; i<= data.size(); i++){
                media = data.getJSONObject(i-1);
                media.put("index",i);
                data.set(i-1,media);//为每个消息加上序号
            }
        }else{
            Collection<Visitor> empty = new ArrayList<>();
            data = JSONArray.parseArray(JSON.toJSONString(empty));
        }
        JSONObject responseMessage = new JSONObject();
        responseMessage.put("code",200);
        responseMessage.put("msg","");
        responseMessage.put("count",count);
        responseMessage.put("data",data);
        System.out.println(responseMessage);
        return responseMessage;
    }
}
